package com.crm.controller;

import com.crm.entity.PageBean;
import com.crm.entity.User;
import com.crm.service.UserService;
import com.crm.util.ResponseUtil;
import com.crm.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户Controller层
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request) {
        User resultUser = userService.login(user);
        if (resultUser == null) {
            request.setAttribute("user", user);
            request.setAttribute("errorMsg", "用户名或密码错误！");
            return "login";
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("currentUser", resultUser);
            return "redirect:/main.jsp";
        }
    }

    @RequestMapping("/list")
    public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, User s_user, HttpServletResponse response) throws IOException {
        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userName", StringUtil.formatLike(s_user.getUserName()));
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<User> userList = userService.find(map);
        Long total = userService.getTotal(map);
        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(userList);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }

    @RequestMapping("/save")
    public String save(User user, HttpServletResponse response) throws IOException {
        JSONObject jsonObject = new JSONObject();
        if (user.getId() == null) {
            userService.add(user);
            jsonObject.put("success", true);
        } else {
            userService.update(user);
            jsonObject.put("success", true);
        }
        ResponseUtil.write(response, jsonObject);
        return null;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "ids") String ids, HttpServletResponse response) throws IOException {
        String[] idsStrArr = ids.split(",");
        for (String idStr : idsStrArr) {
            userService.delete(Integer.parseInt(idStr));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", true);
        ResponseUtil.write(response, jsonObject);
        return null;
    }

    /**
     * 获取客户经理信息 下拉框数据用到
     *
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/customerManagerComboList")
    public String customerManagerComboList(HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roleName", "客户经理");
        List<User> userList = userService.find(map);
        JSONArray row = JSONArray.fromObject(userList);
        ResponseUtil.write(response, row);
        return null;
    }

    /**
     * 获取客户经理信息 下拉框数据用到
     *
     * @param id
     * @param password
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/modifyPassword")
    public String modifyPassword(Integer id, String password, HttpServletResponse response) throws Exception {
        User user = new User();
        user.setId(id);
        user.setPassword(password);
        int resultTotal = userService.update(user);
        JSONObject result = new JSONObject();
        if (resultTotal > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 用户注销
     *
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) throws Exception {
        session.invalidate();
        return "redirect:/login.jsp";
    }
}
